from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import os
from models import Committer
from forms import CommitterForm

class ListHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Committers',
            'section': 'People',
            'people': Committer.all(),
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/people.html')
        self.response.out.write(template.render(path, context))

class ItemHandler(webapp.RequestHandler):

    def get(self, key):
        committer = Committer.get(key)
        context = {
            'title': 'Committer - %s' % (committer.name, ),
            'committer': committer,
            'section': 'People',
        }
        path = os.path.join(os.path.dirname(__file__), 
            '../templates/person.html')
        self.response.out.write(template.render(path, context))

    def post(self, key):
        committer = Committer.get(key)
        data = CommitterForm(data=self.request.POST, instance=committer)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/people/%s/' % (entity.key(), ))
        else:
            context = {
                'title': 'Create a new Committer',
                'form': data,
                'action': '/people/edit/%s/' % (key, ),
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class CreateHandler(webapp.RequestHandler):

    def get(self):
        context = {
            'title': 'Create a new Committer',
            'form': CommitterForm(),
            'action': '/Committers/create/',
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

    def post(self):
        data = CommitterForm(data=self.request.POST)
        if data.is_valid():
            # Save the data, and redirect to the view page
            entity = data.save(commit=False)
            entity.put()
            self.redirect('/Committers/%s/' % (entity.key(), ))
        else:
            context = {
                'title': 'Create a new Committer',
                'form': data,
                'action': '/people/create/',
            }
            path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
            self.response.out.write(template.render(path, context))

class EditHandler(webapp.RequestHandler):

    def get(self, key):
        committer = Committer.get(key)
        context = {
            'title': 'Edit Committer %s' % (committer.name, ),
            'form': CommitterForm(instance=committer),
            'action': '/people/%s/' % (committer.key(), ),
        }
        path = os.path.join(os.path.dirname(__file__), '../templates/form.html')
        self.response.out.write(template.render(path, context))

class DeleteHandler(webapp.RequestHandler):

    def post(self, key):
        committer = Committer.get(key)
        committer.delete()
        self.redirect('/people/')
        
def main():
    application = webapp.WSGIApplication([
        ('/people/', ListHandler),
        ('/people/create/', CreateHandler),
        (r'/people/edit/(.+)/', EditHandler),
        (r'/people/delete/(.+)/', DeleteHandler),
        (r'/people/(.+)/', ItemHandler),
        ], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
